﻿<%@ include file="/WEB-INF/headline/start.jspf" %><html>
<head>
	<%@ include file="/WEB-INF/jspf/meta.jspf" %>
	<title>极客头条</title>
	<link href="${pageContext.request.contextPath}/assets/jqm/jquery.mobile-1.4.5.min.css" rel="stylesheet">
	<script src="${pageContext.request.contextPath}/js/jquery.min.js" type="text/javascript"></script>
	<script src="${pageContext.request.contextPath}/assets/jqm/jquery.mobile-1.4.5.min.js" type="text/javascript"></script>
</head>
<body>
	<div data-role="page">
		<header data-role="header" id="header" data-theme="b">
			<h1>极客头条</h1>
			<nav data-role="navbar">
				<ul>
					<c:set var="site" value="${param.site}" />
					<% 
						Map<String,String> pages= new LinkedHashMap<String,String>();
						pages.put("所有","");
						pages.put("csdn","csdn");
						pages.put("toutiao","toutiao");
						pages.put("segmentfault","segmentfault");
						pages.put("推酷","tuicool");

						request.setAttribute("pages",pages);
					%>
					<c:forEach var="page" items="${pages}">
						<c:choose>
							<c:when test="${(empty page.value && empty site) || page.value==site}">
								<li><a data-ajax="false" href="?site=${page.value}" class="ui-btn-active">${page.key}</a></li>
							</c:when>
							<c:otherwise>
								<li><a data-ajax="false" href="?site=${page.value}">${page.key}</a></li>
							</c:otherwise>
						</c:choose>
					</c:forEach>
				</ul>
			</nav>
		</header>
		<div data-role="content">
			<ul data-role="listview">
				<c:set var="page" value="${param.page}" />
				<c:set var="pageSize" value="${param.size}" scope="request"/>
				<c:if test="${empty page}">
					<c:set var="page" value="1" />
				</c:if>
				<c:if test="${empty pageSize}">
					<c:set var="pageSize" value="20" scope="request"/>
				</c:if>
				<c:choose>
					<c:when test="${empty site}">
						<sql:query var="items" dataSource="${headline}">
							select a.*,b.username as created_by,c.up_count from headline_link a inner join auth_user b on a.created_by_id=b.id left join headline_linkaddon c on a.id=c.link_id order by a.id desc limit ${pageSize} offset ${(page-1)*pageSize}
						</sql:query>
						<sql:query var="total" dataSource="${headline}">
							select count(1) as total from headline_link 
						</sql:query>
					</c:when>
					<c:otherwise>
						<sql:query var="items" dataSource="${headline}">
							select a.*,b.username as created_by,c.up_count from headline_link a inner join auth_user b on a.created_by_id=b.id left join headline_linkaddon c on a.id=c.link_id inner join external_host d on d.id=a.site_id and d.slug=? order by a.id desc limit ${pageSize} offset ${(page-1)*pageSize}
							<sql:param>${ site }</sql:param>
						</sql:query>
						<sql:query var="total" dataSource="${headline}">
							select count(1) as total from headline_link a inner join external_host d on d.id=a.site_id and d.slug=?
							<sql:param>${ site }</sql:param>
						</sql:query>
					</c:otherwise>
				</c:choose>
				<c:set var="total" value="${total.rows[0].total}" scope="request" />
				<% 
					int total = Integer.parseInt(request.getAttribute("total").toString());
					int size = Integer.parseInt(request.getAttribute("pageSize").toString());
					request.setAttribute("totalPages",(int)Math.ceil(total*1.0/size*1.0));
				%>
				<c:forEach var="item" items="${items.rows}" varStatus="status">
					<li>
						<a href="${ item.true_url }" data-ajax="false" target="_blank">
							<h3 class="ui-li-heading">${ item.title }</h3>
							<p class="ui-li-desc">${ item.created_by} 发布于 <time datetime="${item.created_at}">${item.created_at}前</time>(${item.up_count}个赞)</p>
						</a>
					</li>
				</c:forEach>
			</ul>
		</div>
		<div>
			<c:if test="${page>1}">
				<a href="?site=${site}&page=${ page-1 }&size=${pageSize}" data-role="button" data-inline="true" data-icon="arrow-l">Prev</a>
			</c:if>
			<c:if test="${page<totalPages}">
				<a href="?site=${site}&page=${ page+1 }&size=${pageSize}" data-role="button" data-inline="true" data-icon="arrow-r" data-iconpos="right">Next</a> <br />
			</c:if>
			页次 ${page}/${totalPages}
		</div>
		<footer data-role="footer" data-theme="a">
			<p>&copy; 2008~<fmt:formatDate value="${now}" pattern="yyyy" /> <a href="http://www.yitree.pw">yitree.pw</a></p>
			<div data-role="controlgroup" data-type="horizontal">
				<a href="#" data-ajax="false" data-role="button" data-icon="home">头条</a>
				<a href="#" data-ajax="false" data-role="button">糗事</a>
				<a href="#" data-ajax="false" data-role="button">阅读</a>
				<a href="#" data-ajax="false" data-role="button">博客</a>
			</div>
		</footer>
	</div>
</body>
</html>
